package com.eurasia.assignment.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eurasia.assignment.controller.dto.ClassRequest;
import com.eurasia.assignment.controller.dto.ClassResponse;
import com.eurasia.assignment.domain.Class;
import com.eurasia.assignment.service.ClassService;

/**
 * 班级控制器
 */
@RestController
@RequestMapping("/classes")
public class ClassControllerWithDTO {

    private final ClassService classService;

    public ClassControllerWithDTO(ClassService classService) {
        this.classService = classService;
    }

    /**
     * 创建班级
     * POST http://localhost:8080/classes
     */
    @PostMapping("")
    public ResponseEntity<ClassResponse> createClass(@RequestBody ClassRequest request) {
        Class classObj = Class.create(
            request.getClassName(),
            request.getGrade(),
            request.getMajor(),
            request.getTutor()
        );
        
        if (request.getStudentCount() != null) {
            classObj.setStudentCount(request.getStudentCount());
        }
        
        if (request.getRemark() != null) {
            classObj.setRemark(request.getRemark());
        }
        
        Class createdClass = classService.createClassComplete(classObj);
        return ResponseEntity.ok(ClassResponse.fromDomain(createdClass));
    }
} 